import { createRouter, createWebHistory } from 'vue-router'
//固定路由表
import routes from './constRouters'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})


//设置全局前置守卫函数
router.beforeEach((to, from, next) => {
  //获取token
  var token = localStorage.getItem('token')
  if (token) {
    if (to.path == '/login') {
      next('/')
    }
    else {
      next()
    }
  }
  else {
    if (to.path == '/login' || to.path == '/register') {
      next()
    }
    else {
      next('/login')
    }
  }
  //动态修改网页标题
  document.title = to.meta.title
  //进度条
  NProgress.start()
})


router.afterEach((to, from) => {
  NProgress.done()
})
export default router
